package com.dao.impl;

import com.bean.Student;
import com.dao.StudentDao;
import com.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//接口名+Impl=当前类名
public class StudentDaoImpl extends DruidUtil implements StudentDao {
    @Override
    public List<Student> getall() {
        List<Student> list=new ArrayList<>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection = getConnection();
            preparedStatement= connection.prepareStatement("select*from student");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Student student = new Student();
                student.setStuid(resultSet.getInt("stuid"));
                student.setStuname(resultSet.getString("stuname"));
                student.setAge(resultSet.getInt("age"));
                student.setSex(resultSet.getInt("sex"));
                list.add(student);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }

        return list;
    }
}
